package com.yanqu.road.dao.impl.activity.pasture.channel;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pasture.channel.ChannelPastureUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelPastureUserDataDaoImpl extends TempDataObjectDao<ChannelPastureUserData> {


    public static String getChannelPastureUserDataSql = "select * from `t_n_activity_pasture_user_data` where `activityId`=? ";
    public Map<Long, ChannelPastureUserData> getChannelPastureUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ChannelPastureUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getChannelPastureUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelPastureUserData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateChannelPastureUserDataSql = "update `t_n_activity_pasture_user_data` set groupId=?,userBaseInfo=?,score=?,round=?,badge=?,zoneId=?,channelId=?,lastUpdateTime=?,lastUpdateTime2=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ChannelPastureUserData channelPastureUserData) {
        channelPastureUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, channelPastureUserData.getUserBaseInfo() == null ? "" : JSON.toJSONString(channelPastureUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getBadge()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getZoneId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getLastUpdateTime2()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getUserId()));
        if (getDb().executeNoneQuery(updateChannelPastureUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertChannelPastureUserDataSql = "insert into `t_n_activity_pasture_user_data`(activityId,userId,groupId,userBaseInfo,score,round,badge,zoneId,channelId,lastUpdateTime,lastUpdateTime2) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ChannelPastureUserData channelPastureUserData) {
        channelPastureUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, channelPastureUserData.getUserBaseInfo() == null ? "" : JSON.toJSONString(channelPastureUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getRound()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getBadge()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getZoneId()));
        para.put(i++, new DbParameter(Types.INTEGER, channelPastureUserData.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, channelPastureUserData.getLastUpdateTime2()));
        if (getDb().executeNoneQuery(insertChannelPastureUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ChannelPastureUserData parseData(ResultSet rs) throws SQLException {
        ChannelPastureUserData channelPastureUserData = new ChannelPastureUserData();
        channelPastureUserData.setActivityId(rs.getInt("activityId"));
        channelPastureUserData.setUserId(rs.getLong("userId"));
        channelPastureUserData.setGroupId(rs.getInt("groupId"));
        String baseInfoStr = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseInfoStr)){
            channelPastureUserData.setUserBaseInfo(JSON.parseObject(baseInfoStr, UserBaseInfo.class));
        }
        channelPastureUserData.setScore(rs.getLong("score"));
        channelPastureUserData.setRound(rs.getInt("round"));
        channelPastureUserData.setBadge(rs.getLong("badge"));
        channelPastureUserData.setZoneId(rs.getInt("zoneId"));
        channelPastureUserData.setChannelId(rs.getInt("channelId"));
        channelPastureUserData.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        channelPastureUserData.setLastUpdateTime2(rs.getLong("lastUpdateTime2"));
        return channelPastureUserData;
    }
}
